package com.nuc.bigdata.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nuc.bigdata.exception.BusinessException;
import com.nuc.bigdata.model.ResultCode;
import com.nuc.bigdata.orm.entity.PhotoShareMenu;
import com.nuc.bigdata.orm.feign.PhotoShareMenuFeign;
import com.nuc.bigdata.orm.mapper.PhotoShareMenuMapper;
import com.nuc.bigdata.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author JackLuo
 * @date 2019/11/26
 */
@Service
public class MenuServiceImpl implements MenuService {

    private final PhotoShareMenuMapper photoShareMenuMapper;

    @Autowired
    MenuServiceImpl(PhotoShareMenuMapper photoShareMenuMapper){
        this.photoShareMenuMapper=photoShareMenuMapper;
    }


    /**
     * 查出可用的菜单项
     * @return 菜单项实体的ArrayList
     */
    @Override
    public ArrayList<PhotoShareMenu> listAll() {
        QueryWrapper<PhotoShareMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.orderByDesc("create_time");
        List<PhotoShareMenu> photoShareMenus = photoShareMenuMapper.selectList(queryWrapper);
        return new ArrayList<>(photoShareMenus);
    }

    /**
     * 查出可用菜单项(前端展示)
     * @return  展示菜单项ArrayList
     */
    @Override
    public ArrayList<PhotoShareMenuFeign> listAllFeign() {
        ArrayList<PhotoShareMenu> menuArrayList = listAll();
        ArrayList<PhotoShareMenuFeign> menuFeignArrayList = new ArrayList<>();
        for (PhotoShareMenu menu:menuArrayList){
            PhotoShareMenuFeign photoShareMenuFeign = transformFeign(menu);
            menuFeignArrayList.add(photoShareMenuFeign);
        }
        return menuFeignArrayList;
    }

    /**
     * 创建一个新的菜单项
     * @param menuItemTitle 菜单项字符串
     * @param userId 用户唯一标识
     */
    @Override
    public void createMenuItem(String menuItemTitle,String userId) {
        QueryWrapper<PhotoShareMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_title",menuItemTitle);
        // 先看是否存在这个菜单项
        PhotoShareMenu menuItem = photoShareMenuMapper.selectOne(queryWrapper);
        if (ObjectUtils.isEmpty(menuItem)) {
            PhotoShareMenu newItem = new PhotoShareMenu();
            newItem.setMenuId(IdUtil.fastSimpleUUID());
            newItem.setMenuTitle(menuItemTitle);
            newItem.setCreatedBy(userId);
            photoShareMenuMapper.insert(newItem);
        }else {
            throw new BusinessException(ResultCode.PHOTO_MENU_IS_EXIST,menuItem);
        }
    }

    /**
     * 移除一个菜单项
     *
     * @param menuItemId 菜单项唯一标识
     */
    @Override
    public void removeMenuItem(String menuItemId) {
        PhotoShareMenu menuItem = selectByMenuId(menuItemId);
        if (ObjectUtils.isEmpty(menuItem)) {
            throw new BusinessException(ResultCode.PHOTO_MENU_NOT_EXIST,null);
        }else {
            QueryWrapper<PhotoShareMenu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("menu_id",menuItemId);
            photoShareMenuMapper.delete(queryWrapper);
        }
    }

    /**
     * 修改菜单项
     *
     * @param menuItemId    菜单项唯一标识
     * @param menuItemTitle 菜单项字符串
     * @param userId        用户唯一标识
     */
    @Override
    public void updateMenuItem(String menuItemId, String menuItemTitle, String userId) {
        PhotoShareMenu menuItem = selectByMenuId(menuItemId);
        if (ObjectUtils.isEmpty(menuItem)) {
            throw new BusinessException(ResultCode.PHOTO_MENU_NOT_EXIST,null);
        }else {
            UpdateWrapper<PhotoShareMenu> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("menu_id",menuItemId);
            menuItem.setMenuTitle(menuItemTitle);
            menuItem.setLastUpdatedBy(userId);
            menuItem.setUpdateTime(new Date());
            photoShareMenuMapper.update(menuItem,updateWrapper);
        }
    }

    /**
     * 根据菜单项唯一标识查询菜单项
     * @param menuItemId 菜单项唯一标识
     * @return 菜单项实体
     */
    private PhotoShareMenu selectByMenuId(String menuItemId) {
        QueryWrapper<PhotoShareMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_id",menuItemId);
        return photoShareMenuMapper.selectOne(queryWrapper);
    }

    /**
     * 将菜单实体转化为前端展示
     * @param photoShareMenu 菜单实体
     * @return 菜单实体feign
     */
    private PhotoShareMenuFeign transformFeign(PhotoShareMenu photoShareMenu){
        PhotoShareMenuFeign photoShareMenuFeign = new PhotoShareMenuFeign();
        photoShareMenuFeign.setId(photoShareMenu.getId());
        photoShareMenuFeign.setMenuId(photoShareMenu.getMenuId());
        photoShareMenuFeign.setMenuTitle(photoShareMenu.getMenuTitle());
        return photoShareMenuFeign;
    }
}
